/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.zmime.ZSharedFileInputStream;
/*     */ import com.zimbra.cs.mailbox.DeliveryOptions;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mime.Mime;
/*     */ import com.zimbra.cs.mime.Mime.FixedMimeMessage;
/*     */ import com.zimbra.cs.mime.ParsedMessage;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.cs.store.MailboxBlob;
/*     */ import com.zimbra.cs.store.StoreManager;
/*     */ import com.zimbra.cs.util.JMSession;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.PrintStream;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.Comparator;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ import javax.mail.internet.MimeMultipart;
/*     */ import javax.mail.internet.MimePart;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RemoveAttachments
/*     */   extends MailDocumentHandler
/*     */ {
/*  57 */   private static final String[] TARGET_MSG_PATH = { "m", "id" };
/*  58 */   private static final String[] RESPONSE_ITEM_PATH = new String[0];
/*  59 */   protected String[] getProxiedIdPath(Element request) { return TARGET_MSG_PATH; }
/*  60 */   protected String[] getResponseItemPath() { return RESPONSE_ITEM_PATH; }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException {
/*  63 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  64 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  65 */     OperationContext octxt = getOperationContext(zsc, context);
/*  66 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*  68 */     Element msgElem = request.getElement("m");
/*  69 */     ItemId iid = new ItemId(msgElem.getAttribute("id"), zsc);
/*  70 */     Collection<String> parts = sortPartIds(msgElem.getAttribute("part"));
/*     */     
/*  72 */     Message msg = mbox.getMessageById(octxt, iid.getId());
/*     */     
/*  74 */     InputStream is = null;
/*     */     try {
/*  76 */       MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is = StoreManager.getInstance().getContent(msg.getBlob().getLocalBlob()));
/*  77 */       for (String part : parts)
/*  78 */         stripPart(mm, part);
/*  79 */       mm.saveChanges();
/*     */       
/*  81 */       ParsedMessage pm = new ParsedMessage(mm, msg.getDate(), mbox.attachmentsIndexingEnabled());
/*  82 */       if (msg.isDraft()) {
/*  83 */         msg = mbox.saveDraft(octxt, pm, msg.getId());
/*     */       } else {
/*  85 */         DeliveryOptions dopt = new DeliveryOptions();
/*  86 */         dopt.setFolderId(msg.getFolderId()).setNoICal(true);
/*  87 */         dopt.setFlags(msg.getFlagBitmask()).setTags(msg.getTags());
/*  88 */         if (msg.getConversationId() > 0) {
/*  89 */           dopt.setConversationId(msg.getConversationId());
/*     */         }
/*  91 */         msg = mbox.addMessage(octxt, pm, dopt, null);
/*     */         
/*  93 */         mbox.delete(octxt, iid.getId(), MailItem.Type.MESSAGE);
/*     */       }
/*     */     } catch (IOException ioe) {
/*  96 */       throw ServiceException.FAILURE("error reading existing message blob", ioe);
/*     */     } catch (MessagingException me) {
/*  98 */       throw ServiceException.FAILURE("error reading existing message blob", me);
/*     */     } finally {
/* 100 */       ByteUtil.closeStream(is);
/*     */     }
/*     */     
/* 103 */     Element response = zsc.createElement(MailConstants.REMOVE_ATTACHMENTS_RESPONSE);
/*     */     
/* 105 */     ToXML.encodeMessageAsMP(response, ifmt, octxt, msg, null, -1, true, true, null, true, false, LC.mime_encode_missing_blob.booleanValue());
/* 106 */     return response;
/*     */   }
/*     */   
/*     */   private static class PartIdComparator
/*     */     implements Comparator<String>
/*     */   {
/*     */     public int compare(String o1, String o2)
/*     */     {
/* 114 */       if (o1.equalsIgnoreCase(o2)) return 0;
/* 115 */       if (o1.equals("")) return 1;
/* 116 */       if (o2.equals("")) { return -1;
/*     */       }
/* 118 */       String[] parts1 = o1.split("\\.");String[] parts2 = o2.split("\\.");
/* 119 */       for (int depth = 0; (depth < parts1.length) && (depth < parts2.length); depth++) {
/* 120 */         String subpart1 = parts1[depth];String subpart2 = parts2[depth];
/* 121 */         if ((subpart1.equalsIgnoreCase("TEXT")) && (depth == parts1.length - 1))
/* 122 */           return 1;
/* 123 */         if ((subpart2.equalsIgnoreCase("TEXT")) && (depth == parts2.length - 1)) {
/* 124 */           return -1;
/*     */         }
/* 126 */         int delta = Integer.valueOf(subpart2).intValue() - Integer.valueOf(subpart1).intValue();
/* 127 */         if (delta != 0) {
/* 128 */           return delta;
/*     */         }
/*     */       }
/* 131 */       return parts2.length - parts1.length;
/*     */     }
/*     */   }
/*     */   
/*     */   private static Collection<String> sortPartIds(String partnames) throws ServiceException {
/* 136 */     String[] parts = partnames.split(",");
/*     */     
/* 138 */     if ((parts.length == 1) && (!parts[0].trim().equals("")))
/* 139 */       return Arrays.asList(parts);
/* 140 */     if (parts.length < 2) {
/* 141 */       return Collections.emptyList();
/*     */     }
/*     */     
/* 144 */     Set<String> sorted = new TreeSet(new PartIdComparator(null));
/* 145 */     for (String part : parts) {
/*     */       try {
/* 147 */         if (!part.trim().equals(""))
/* 148 */           sorted.add(part);
/*     */       } catch (NumberFormatException nfe) {
/* 150 */         throw ServiceException.INVALID_REQUEST("invalid part id: " + part, null);
/*     */       }
/*     */     }
/* 153 */     return sorted;
/*     */   }
/*     */   
/*     */   private static void stripPart(MimeMessage mm, String part) throws IOException, MessagingException, ServiceException {
/* 157 */     MimePart mp = mm;
/* 158 */     int dot = (part = part.trim()).lastIndexOf('.');
/* 159 */     if (dot > 0)
/* 160 */       mp = Mime.getMimePart(mm, part.substring(0, dot));
/* 161 */     if (mp == null) {
/* 162 */       throw MailServiceException.NO_SUCH_PART(part);
/*     */     }
/* 164 */     String subpart = dot > 0 ? part.substring(dot + 1) : part;
/* 165 */     if (subpart.equalsIgnoreCase("TEXT")) {
/* 166 */       if (!(mp instanceof MimeMessage))
/* 167 */         throw MailServiceException.NO_SUCH_PART(part);
/* 168 */       mp.setText("");
/*     */     } else {
/*     */       try {
/* 171 */         int partid = Integer.parseInt(subpart);
/* 172 */         if (Mime.getContentType(mp, "text/plain").startsWith("multipart/")) {
/* 173 */           MimeMultipart mmp = Mime.getMultipartContent(mp, mp.getContentType());
/* 174 */           if ((partid <= 0) || (partid > mmp.getCount()))
/* 175 */             throw MailServiceException.NO_SUCH_PART(part);
/* 176 */           mmp.removeBodyPart(partid - 1);
/* 177 */         } else if (((mp instanceof MimeMessage)) && (partid == 1)) {
/* 178 */           mp.setText("");
/*     */         } else {
/* 180 */           throw MailServiceException.NO_SUCH_PART(part);
/*     */         }
/*     */       } catch (NumberFormatException nfe) {
/* 183 */         throw ServiceException.INVALID_REQUEST("invalid part id: " + part, null);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws ServiceException {
/* 189 */     System.out.println(sortPartIds("1"));
/* 190 */     System.out.println(sortPartIds("1,2,3"));
/* 191 */     System.out.println(sortPartIds("1,1,2,1"));
/* 192 */     System.out.println(sortPartIds("1.1,TEXT,1.TEXT,10,2.1"));
/*     */     
/* 194 */     InputStream is = null;
/*     */     try {
/* 196 */       File file = new File(args[0]);
/* 197 */       MimeMessage mm = new Mime.FixedMimeMessage(JMSession.getSession(), is = new ZSharedFileInputStream(file));
/* 198 */       stripPart(mm, "1.2");
/* 199 */       mm.saveChanges();
/* 200 */       mm.writeTo(System.out);
/*     */     } catch (Exception e) {
/* 202 */       e.printStackTrace();
/*     */     } finally {
/* 204 */       ByteUtil.closeStream(is);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/RemoveAttachments.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */